package com.cloudkinto.controller.stock;


import com.cloudkinto.service.stock.vo.ShippingAddressAddReq;
import com.cloudkinto.service.stock.vo.ShippingAddressUpdateReq;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.stock.ShippingAddressService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author zhujiale
 * @since 2022-09-22
 */
@RestController
@Api(tags = "收货地址模块")
@RequestMapping("/api/admin/shipping/address")
public class ShippingAddressController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ShippingAddressController.class);

    @Autowired
    private ShippingAddressService service;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增收货地址", notes = "新增收货地址")
    public SingleResult add(@RequestBody @Validated ShippingAddressAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功",service.add(req,userId,companyId));
    }


    @PutMapping("/update")
    @ApiOperation(value = "收货地址修改", notes = "修改收货地址")
    public SingleResult update(@RequestBody @Validated ShippingAddressUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功",service.delete(id));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }


    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword1", value = "联系人/公司/地址"),
            @ApiImplicitParam(name = "keyword2", value = "邮编/电话"),
            @ApiImplicitParam(name = "areaId", value = "国家id"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "current", value = "当前页")})
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword1 = request.getParameter("keyword1");
        String keyword2 = request.getParameter("keyword2");
        String areaId = request.getParameter("areaId");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("keyword1", keyword1);
        map.put("keyword2", keyword2);
        map.put("areaId", areaId);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("companyId", companyId + "");
        return service.getListPage(map);
    }

}

